package com.xqboss.apps.service.order;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xqboss.apps.domain.order.OrderQuickCopyRecord;
import com.xqboss.apps.domain.user.UserIdentityPermBasic;
import com.xqboss.apps.mapper.order.OrderQuickCopyRecordMapper;
import com.xqboss.apps.service.user.SysUserHotbeansRecordService;
import com.xqboss.apps.service.user.UserIdentityPermBasicService;
import com.xqboss.chain.domain.chain.dto.BaseRequest;
import com.xqboss.common.core.domain.entity.SysDictData;
import com.xqboss.system.mapper.SysDictDataMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 木鱼
 * @since 2023-12-15
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class OrderQuickCopyRecordService extends ServiceImpl<OrderQuickCopyRecordMapper, OrderQuickCopyRecord> {

    @Autowired
    private SysDictDataMapper dictDataMapper;
    @Autowired
    private SysUserHotbeansRecordService sysUserHotbeansRecordService;
    @Autowired
    private UserIdentityPermBasicService userIdentityPermBasicService;

    public List<OrderQuickCopyRecord> selectBasicList(BaseRequest request) {
        return baseMapper.selectList(Wrappers.<OrderQuickCopyRecord>lambdaQuery());
    }

    public OrderQuickCopyRecord selectByIdBasic(Long id) {
        return baseMapper.selectById(id);
    }

    public boolean saveBasic(OrderQuickCopyRecord record) {
        return baseMapper.insert(record) > 0;
    }

    public boolean updateBasic(OrderQuickCopyRecord record) {
        return baseMapper.updateById(record) > 0;
    }

    public boolean removeBasic(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    /**
     * 验证用户是否属于快速抄货白名单
     * 如果当前用户身份>=经商者,则默认开启.
     * @param userId
     * @return
     */
    public Boolean getIsWhiteList(Long userId) {
        // 查询用户身份是否具有快速抄货权限
        //
        Long identityId = sysUserHotbeansRecordService.getUserIdentity(userId);
        LambdaQueryWrapper<UserIdentityPermBasic> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserIdentityPermBasic::getIdentityId,identityId);
        // 查出该身份配置是否有权限
        UserIdentityPermBasic permBasic = userIdentityPermBasicService.getBaseMapper().selectOne(wrapper);
        Boolean isQuickCopy = permBasic.getIsQuickCopy();

        // 查询用户是否在快速抄货白名单
        QueryWrapper<SysDictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().
                eq(SysDictData::getDictType,"quick_copy_whitelist").
                eq(SysDictData::getDictValue,userId);
        SysDictData sysDictData = dictDataMapper.selectOne(queryWrapper);

        return isQuickCopy || sysDictData != null;
    }



}